﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using EShop.Common;
using EShop.BLL;

namespace EShop.Client
{
    public partial class frmMerchandiseBindManager : frmBase
    {
        public frmMerchandiseBindManager()
        {
            InitializeComponent();
        }

        int TotalRow = 0;
        //int pageSize = oShopConfigInfo.PageSize;
        int pageSize = 10;
        int pageCount = 0;
        int pageCurrent = 0;

        DataTable datasource = new DataTable();
        MerchandiseBindBLL oMerchandiseBindBLL = new MerchandiseBindBLL();

        private void frmMerchandiseBindManager_Load(object sender, EventArgs e)
        {
            btnAddNewItem.Enabled = true;
            btnDeleteItem.Enabled = true;
            btnMoveFirstItem.Enabled = true;
            btnMoveLastItem.Enabled = true;
            btnMoveNextItem.Enabled = true;
            btnMovePreviousItem.Enabled = true;
            txtPositionItem.Enabled = true;
            lblCountItem.Enabled = true;
            LoadData();
        }

        private void LoadData()
        {
            datasource = oMerchandiseBindBLL.MerchandiseBindQuery(null);
            Init();
        }

        private void Init()
        {
            dgvMerchandiseBind.Rows.Clear();
            TotalRow = datasource.Rows.Count;
            if (TotalRow <= 0) return;
            pageCount = TotalRow / pageSize;
            if (TotalRow % pageSize != 0) pageCount += 1;
            AsyncMethod.ControlsEndueValueEventHandler OnToolStripLabelSetValue = new AsyncMethod.ControlsEndueValueEventHandler(OnToolStripLabelEndureValue);
            this.BeginInvoke(OnToolStripLabelSetValue, new object(),
                new AsyncMethod.EndueValueEventArgs(lblCountItem, "/"+pageCount.ToString(), OnToolStripLabelSetValue));
            BindData();
        }

        private void BindData()
        {
            AsyncMethod.ControlsEndueValueEventHandler OnToolStripTextBoxSetValue = new AsyncMethod.ControlsEndueValueEventHandler(OnToolStripTextBoxEndureValueEvent);
            this.BeginInvoke(OnToolStripTextBoxSetValue, new object(),
                new AsyncMethod.EndueValueEventArgs(txtPositionItem, (pageCurrent + 1).ToString(), OnToolStripTextBoxSetValue));
            int iStart = 0, iEnd = 0;
            if (pageCurrent >= (pageCount - 1))
            {
                iStart = (pageCount-1) * pageSize;
                iEnd = TotalRow;
            }
            else
            {
                iStart = pageCurrent * pageSize;
                iEnd = iStart + pageSize;
            }

            int index = -1;
            for (int i = iStart; i < iEnd; i++)
            {
                index = dgvMerchandiseBind.Rows.Add();
                dgvMerchandiseBind.Rows[index].Cells["BindID"].Value = datasource.Rows[i]["BindID"];
                dgvMerchandiseBind.Rows[index].Cells["BindName"].Value = datasource.Rows[i]["BindName"];
                dgvMerchandiseBind.Rows[index].Cells["UnitName"].Value = datasource.Rows[i]["UnitName"];
                try
                {
                    dgvMerchandiseBind.Rows[index].Cells["Quantity"].Value = Convert.ToInt32(datasource.Rows[i]["Quantity"]);
                }
                catch { dgvMerchandiseBind.Rows[index].Cells["Quantity"].Value = 0; }
                try
                {
                    dgvMerchandiseBind.Rows[index].Cells["SalePrice"].Value = Common.FormatManager.FormatDecimal(Convert.ToDecimal(datasource.Rows[i]["SalePrice"]));
                }
                catch
                {
                    dgvMerchandiseBind.Rows[index].Cells["SalePrice"].Value = Common.FormatManager.FormatDecimal(0m);
                }
                dgvMerchandiseBind.Rows[index].Cells["SellPrice"].Value = Common.FormatManager.FormatDecimal(Convert.ToDecimal(datasource.Rows[i]["SellPrice"]));
                dgvMerchandiseBind.Rows[index].Cells["CreateTime"].Value = datasource.Rows[i]["CreateTime"];
            }
            
        }

        private void btnMoveFirstItem_Click(object sender, EventArgs e)
        {
            if (pageCurrent == 0) return;
            pageCurrent = 0;
            BindData();
        }

        private void btnMovePreviousItem_Click(object sender, EventArgs e)
        {
            if (pageCurrent == 0) return;
            pageCurrent -= 1;
            BindData();
        }

        private void btnMoveNextItem_Click(object sender, EventArgs e)
        {
            if (pageCurrent >= pageCount - 1) return;
            pageCount += 1;
            BindData();
        }

        private void btnMoveLastItem_Click(object sender, EventArgs e)
        {
            if (pageCurrent >= pageCount - 1) return;
            pageCurrent = pageCount - 1;
            BindData();
        }

        private void btnAddNewItem_Click(object sender, EventArgs e)
        {
            frmMerchandiseBindAppend oMerchandiseBindAppend = new frmMerchandiseBindAppend();
            if (oMerchandiseBindAppend.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void 添加绑定商品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = dgvMerchandiseBind.SelectedRows[0].Index;
            try
            {
                string BindID = dgvMerchandiseBind.Rows[index].Cells["BindID"].Value.ToString();
                string MerchandiseName = dgvMerchandiseBind.Rows[index].Cells["BindName"].Value.ToString();
                if (string.IsNullOrEmpty(BindID) || string.IsNullOrEmpty(MerchandiseName)) return;
                frmMerchandiseBindDetail oMerchandiseBindDetail = new frmMerchandiseBindDetail(BindID, MerchandiseName);
                if (oMerchandiseBindDetail.ShowDialog() == DialogResult.OK)
                    LoadData();
            }
            catch { }
        }

        private void 查看修改绑定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = dgvMerchandiseBind.SelectedRows[0].Index;
            try
            {
                string BindID = dgvMerchandiseBind.Rows[index].Cells["BindID"].Value.ToString();
                if (string.IsNullOrEmpty(BindID)) return;
                frmMerchandiseBindEdit oMerchandiseBindEdit = new frmMerchandiseBindEdit(BindID);
                if (oMerchandiseBindEdit.ShowDialog() == DialogResult.OK)
                    LoadData();
            }
            catch { }
        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {
            string BindID=string.Empty;
            string BindName = string.Empty;
            try 
            {
                BindID = dgvMerchandiseBind.SelectedRows[0].Cells["BindID"].Value.ToString();
                BindName = dgvMerchandiseBind.SelectedRows[0].Cells["BindName"].Value.ToString();
            }
            catch
            { }
            if (string.IsNullOrEmpty(BindID)||string.IsNullOrEmpty(BindName)) return;
            string strMsg = string.Empty;
            strMsg += "您正在执行删除套装操作:\t\t\t\t\r\n";
            strMsg += "\r\n\t绑定的ID:\t" + BindID;
            strMsg += "\r\n\t绑定的名称:\t" + BindName;
            strMsg += "\t\n\r\n您确定要删除吗?";
            if (MessageBox.Show(strMsg, "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                if (oMerchandiseBindBLL.MerchandiseBindDelete(BindID))
                {
                    MessageBox.Show("操作成功.");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("操作失败,请重试.");
                }
            }
        }

        private void 删除指定绑定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnDeleteItem_Click(sender, e);
        }
    }
}
